package com.bilin.coin.network.builder;


import com.bilin.coin.ApiConstant;
import com.bilin.coin.network.RetrofitConfig;
import com.bilin.coin.network.api.RetrofitApi;
import com.bilin.coin.utils.GsonUtil;
import com.bilin.coin.utils.SharedPreferencesUtils;

import java.util.Map;


import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/3/28.
 */

public class PostBuilder extends RetrofitRequestBuilder<String, PostBuilder> implements HasParamsable {


    public PostBuilder() {}

    public PostBuilder(String baseurl) {
        this.baseUrl = baseurl;
    }

    @Override
    public Observable<String> build() {
        String cookie = SharedPreferencesUtils.getInstance().getString("Cookie", "");
        Map<String, Object> mapCookie = GsonUtil.GsonToMaps(cookie);
        StringBuffer buffer = new StringBuffer();
        if(mapCookie!=null) {
            for (String key : mapCookie.keySet()) {
                buffer.append(key).append("=").append(mapCookie.get(key)).append("; ");
            }
        }
        headers.put("Cookie", buffer.toString() + "lang="+ SharedPreferencesUtils.getInstance().getString("lang","cn")+";Path=/");
        headers.put(ApiConstant.CSRF_TOKEN, SharedPreferencesUtils.getInstance().getString(ApiConstant.CSRF_TOKEN, ""));
        headers.put(ApiConstant.RegisterToken, SharedPreferencesUtils.getInstance().getString(ApiConstant.RegisterToken, ""));
        headers.put("device", "android");

        return RetrofitConfig
                .initRetrofitConfig()
                .baseUrl(baseUrl)
                .build()
                .create(RetrofitApi.class)
                .post(url,headers,params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public PostBuilder params(Map<String, String> params) {
        this.params=params;
        return this;
    }

    @Override
    public PostBuilder addParams(String key, String val) {
        params.put(key, val);
        return this;
    }
}
